﻿
    using Chain.BLL;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public partial class goodorders : Page
    {

        private int totalPoint;
        private decimal TotalSubtotal;

        private void BindMemInfo(int memID)
        {
            Chain.Model.Mem mem = new Chain.BLL.Mem().GetModel(memID);
            this.memname.Value = mem.MemName;
            this.telnumber.Value = mem.MemMobile;
            this.address.Value = mem.MemAddress;
        }

        private void DataBindGoodCard(HttpCookie cookie)
        {
            Chain.BLL.MicroWebsiteGoods MicroWebsiteGoodsBll = new Chain.BLL.MicroWebsiteGoods();
            StringBuilder sbWhere = new StringBuilder("MicroGoodsID in(");
            string[] keys = HttpUtility.UrlDecode(cookie.Value).Trim().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in keys)
            {
                int GiftID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
                Convert.ToInt32(item.Split(new char[] { ',' })[1]);
                sbWhere.AppendFormat("{0},", GiftID);
            }
            sbWhere.Append("0)");
            DataTable dt = MicroWebsiteGoodsBll.GetList(sbWhere.ToString()).Tables[0];
            dt = this.UpDateTable(dt, keys);
            this.rptGoodsinventory.DataSource = dt;
            this.rptGoodsinventory.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int memID = string.IsNullOrEmpty(base.Request.Cookies["uid"].ToString()) ? 0 : int.Parse(base.Request.Cookies["uid"].Value);
            this.BindMemInfo(memID);
            HttpCookie cookie = base.Request.Cookies["goodlist"];
            if ((cookie != null) && !string.IsNullOrEmpty(cookie.Value))
            {
                this.DataBindGoodCard(cookie);
                this.totalMoney.InnerText = this.TotalSubtotal.ToString("0.00");
                this.MicroPoint.Value = this.totalPoint.ToString();
            }
        }

        private DataTable UpDateTable(DataTable dt, string[] keys)
        {
            dt.Columns.Add("ConvertNumber");
            dt.Columns.Add("Subtotal");
            foreach (string item in keys)
            {
                int MicroGoodsID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
                int ConvertNumber = Convert.ToInt32(item.Split(new char[] { ',' })[1]);
                DataRow row = dt.Select("MicroGoodsID=" + MicroGoodsID)[0];
                row["ConvertNumber"] = ConvertNumber;
                row["Subtotal"] = ConvertNumber * Convert.ToDecimal(row["MicroPrice"]);
                this.TotalSubtotal += Convert.ToDecimal(row["Subtotal"]);
                this.totalPoint += ConvertNumber * Convert.ToInt32(row["MicroPoint"]);
            }
            return dt;
        }
    }


